﻿using MovingChame.Web.Helpers;
using MovingChame.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MovingChame.Web.Infrastructure
{
    public class TourTouristProvider : ITourTouristProvider
    {
        private static TourTouristProvider current;
        public static TourTouristProvider Current
        {
            get
            {
                if (current == null)
                    current = new TourTouristProvider();
                return current;
            }
        }

        #region Configuration
        private string applicationName;
        public string ApplicationName
        {
            get { return applicationName; }
            set { applicationName = value; }
        }

        private string connectionStringName;
        private string providerName;
        private MovingChameModelContainer db;

        public void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            providerName = name;

            if (config["applicationName"] != null)
                applicationName = config["applicationName"];

            if (config["connectionStringName"] != null)
            {
                connectionStringName = config["connectionStringName"];
                db = new MovingChameModelContainer();
                db.Database.Connection.ConnectionString = ConfigurationHelper.GetConnectionString(connectionStringName);
            }
            else
                db = new MovingChameModelContainer();
        }

        private TourTouristProvider()
        {
            Initialize("TourTouristProvider", new System.Collections.Specialized.NameValueCollection());
        }
        #endregion

        #region Interface Methods
        public int TouristJoinTour(ConcreteTour concreteTour, User tourist, UserType userType)
        {
            var tourTourist = new TourTourist
            {
                ConcreteTour = concreteTour,
                Type = userType,
                User = tourist,
                Attended = false
            };
            db.TourTourists.Add(tourTourist);

            var tour = db.ConcreteTours.SingleOrDefault(o => o.Id == concreteTour.Id);
            int currentNoOfTourists = tour.NumberOfTourists;
            tour.NumberOfTourists = currentNoOfTourists + 1;

            db.SaveChanges();
            return tourTourist.Id;
        }

        public bool TouristAttended(int touristId, int concreteTourId)
        {
            var record = db.TourTourists.Where(o => o.ConcreteTourId == concreteTourId && o.UserId == touristId).SingleOrDefault();
            if (record == null)
                return false;
            record.Attended = true;
            db.SaveChanges();
            return true;
        }

        public bool CancelTouristAttended(int touristId, int concreteTourId)
        {
            var record = db.TourTourists.Where(o => o.ConcreteTourId == concreteTourId && o.UserId == touristId).SingleOrDefault();
            if (record == null)
                return false;
            record.Attended = false;
            db.SaveChanges();
            return true;
        }

        public bool RemoveTouristFromTour(int touristId, int concreteTourId)
        {
            TourTourist record = db.TourTourists.Where(o => o.ConcreteTourId == concreteTourId).SingleOrDefault(o => o.UserId == touristId);
            db.TourTourists.Remove(record);
            db.SaveChanges();
            return true;
        }

        public IQueryable<ConcreteTour> GetToursUserJoined(int touristId)
        {
            return db.TourTourists.Where(o => o.UserId == touristId).Select(o => o.ConcreteTour);
        }
        #endregion
    }
}